package com.fanben100.biz;

import java.io.IOException;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;

import com.fanben100.domain.LoginModel;
import com.fanben100.domain.Operater;
import com.fanben100.domain.Seller;
import com.fanben100.domain.SysConfig;
import com.fanben100.util.DateUtil;
import com.fanben100.util.ProtocalUtil;
import com.fanben100.util.StringUtil;

/**
 * 超市收银员登录
 * 
 * @author CYC
 * 
 */
public class OperaterLogin extends LoginTemplate {

	protected LoginModel findLoginModel(Serializable loginId) {
		LoginModel loginModel = new Operater();
		loginModel.setLoginId("syy");
		loginModel.setLoginPassword("syy");
		loginModel.setRealName("收银员");
		return loginModel;
	}

	protected LoginModel findLoginModel(LoginModel loginModel) {
		String loginResult = null;
		RequestBuilder requestBuilder = new RequestBuilder(
				"http://www.fanben100.net/shop.php");
		ValidLoginModel validModel = (ValidLoginModel) SysConfig.sellerLoginModel;
		if (validModel.getLoginModel() != null
				&& validModel.getLoginModel() instanceof Seller) {
			Integer sellerId = ((Seller) validModel.getLoginModel())
					.getSellerId();
			requestBuilder.build("seller_id", String
					.valueOf(sellerId));
		}
		
		requestBuilder.build("class", "super");
		requestBuilder.build("method", "opp_login");
		requestBuilder.build("name", String.valueOf(loginModel.getLoginId()));
		requestBuilder.build("password", loginModel.getLoginPassword());
		try {
			loginResult = ProtocalUtil.httpRequest(
					new URL(requestBuilder.toString()), null);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
			loginResult = "请求超时，请检查网络连通情况 " + DateUtil.getCurrentDate();
		}

		ValidLoginModel validLoginModel = new ValidLoginModel(loginModel);
		validLoginModel.setValidMessage(StringUtil.trimAll(loginResult));
		return validLoginModel;
	}
}
